<?php
/**
 * CategoryController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-13 17:23:49
 * @modified    2023-02-13 17:23:49
 *
 */

namespace App\Http\Controllers;

use App\Repositories\Product\CategoryRepo;
use Illuminate\Http\Request;

class CategoryController extends BaseController
{
    /**
     * @OA\Get(
     *     path="/categories",
     *     tags={"Category"},
     *     summary="获取商品分类列表",
     *     description="获取商品分类列表",
     *     operationId="category_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="name", in="query", description="分类名称", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="status", in="query", description="状态", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10, type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index(Request $request)
    {
        $categories = CategoryRepo::getList();
        return list_message("获取成功", true, $categories);
    }

    /**
     * @OA\Get(
     *     path="/categories/tree",
     *     tags={"Category"},
     *     summary="获取分类树列表",
     *     description="获取分类树列表",
     *     operationId="category_category",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="name", in="query", description="分类名称", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function tree(Request $request)
    {
        $categories = CategoryRepo::tree();
        return message("获取成功", true, $categories);
    }

    /**
     * @OA\Get(
     *     path="/categories/{id}",
     *     tags={"Category"},
     *     summary="获取商品分类详情",
     *     description="获取商品分类详情",
     *     operationId="category_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="分类ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function show(Request $request, $id)
    {
        try {
            $category = CategoryRepo::fetch($id);
            return message("获取成功", true, $category);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/categories",
     *     tags={"Category"},
     *     summary="添加商品分类",
     *     description="添加商品分类",
     *     operationId="category_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"parent_id", "name", "sort_order", "status"},
     *              @OA\Property(property="parent_id", type="integer", example=0, description="父级分类ID"),
     *              @OA\Property(property="name", type="string", example="", description="分类名称"),
     *              @OA\Property(property="sort_order", type="integer", example=0, description="排序"),
     *              @OA\Property(property="status", type="integer", example=1, description="排序")
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store(Request $request)
    {
        try {
            $category = CategoryRepo::store(0);
            return message("创建成功", true, $category);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Put(
     *     path="/categories/{id}",
     *     tags={"Category"},
     *     summary="更新商品分类",
     *     description="更新商品分类",
     *     operationId="category_update",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(
     *         name="id",
     *         in="path",
     *         description="分类ID",
     *         required=false,
     *         @OA\Schema(
     *             default="",
     *             type="integer"
     *         )
     *     ),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"parent_id", "name", "sort_order", "status"},
     *              @OA\Property(property="parent_id", type="integer", example=0, description="父级分类ID"),
     *              @OA\Property(property="name", type="string", example="", description="分类名称"),
     *              @OA\Property(property="sort_order", type="integer", example=0, description="排序"),
     *              @OA\Property(property="status", type="integer", example=1, description="排序")
     *          )
     *     ),
     *     @OA\Response(
     *          response=200,
     *          description="请求成功"
     *     ),
     *     @OA\Response(
     *          response=401,
     *          description="鉴权失败"
     *     ),
     *     @OA\Response(
     *          response=422,
     *          description="请求异常"
     *     )
     * )
     */
    public function update(Request $request, $id)
    {
        try {
            $category = CategoryRepo::store($id);
            return message("更新成功", true, $category);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Delete(
     *     path="/categories/{id}",
     *     tags={"Category"},
     *     summary="更新商品分类",
     *     description="更新商品分类",
     *     operationId="category_delete",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(
     *         name="id",
     *         in="path",
     *         description="分类ID",
     *         required=false,
     *         @OA\Schema(
     *             default="",
     *             type="integer"
     *         )
     *     ),
     *     @OA\Response(
     *          response=200,
     *          description="请求成功"
     *     ),
     *     @OA\Response(
     *          response=401,
     *          description="鉴权失败"
     *     ),
     *     @OA\Response(
     *          response=422,
     *          description="请求异常"
     *     )
     * )
     */
    public function destroy(Request $request, $id)
    {
        try {
            CategoryRepo::destroy($id);
            return message("删除成功", true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }
}